package dao;

import java.util.ArrayList;
import java.util.List;


import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import beans.Alternativa;
import beans.Questao;
import beans.Servico;

import android.util.Log;





public class ReturnJson extends Thread {
	private String URL = "";
	private String result = "2";
	private long idQuestao = 0;
	Object parent = null;
	private static String TOKEN = "9cpgmxjW5VeNSsqUrEIChd276dTwe0KX";
	
	private List<Servico> listServicos = new ArrayList<Servico>();
	private List<Questao> listQuestoes = new ArrayList<Questao>();

	private boolean returnServicos;
	private boolean returnQuestoes;
	



	public ReturnJson(Object caller) {
		parent = caller;
	}

	private HttpClient Client = new DefaultHttpClient();

	public void run() {
		try {
			synchronized (parent) {
				String setServerString = "";
				HttpGet httpget = new HttpGet(URL);
				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				setServerString = Client.execute(httpget, responseHandler);
				
				if(isReturnServicos()){
					servicoJsonToObjects(setServerString);
					//setReturnQuestoes(false);
				}else if(isReturnQuestoes()){

					questoesJsonToObjects(setServerString);
				}
							
				parent.notify();
			}
			
		} catch (Throwable t) {
			// just end the background thread
			Log.i("Animation", "Thread  exception " + t);

		}

	}
	
	public void servicoJsonToObjects(String result){
		
		String jsonConsulta = result;
		try{
		JSONObject jsonObject = new JSONObject(jsonConsulta);
		JSONArray servicosArray = jsonObject.getJSONArray("servicos");

		
		
		for (int i=0; i<servicosArray.length(); i++) {
			
			Servico servico = new Servico();
			JSONObject jsonObjectServico = servicosArray.getJSONObject(i);
			
			servico.setId(jsonObjectServico.getInt("id"));
			servico.setNome(jsonObjectServico.getString("nome"));
			servico.setDescricao(jsonObjectServico.getString("descricao"));
			servico.setQuantidadeQuestoes(jsonObjectServico.getInt("quantidade_questoes"));
			servico.setValor(jsonObjectServico.getDouble("valor"));
			
			this.listServicos.add(servico);

		}
		
	
		
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	}
	public void questoesJsonToObjects(String result){
		
		String jsonConsulta = result;
		try{
			JSONObject jsonObject = new JSONObject(jsonConsulta);
			JSONArray questoesArray = jsonObject.getJSONArray("questoes");		
		
			for (int i=0; i<questoesArray.length(); i++) {
				
				Questao questao = new Questao();
				JSONObject jsonObjectQuestao = questoesArray.getJSONObject(i);
				
				questao.setId(jsonObjectQuestao.getJSONObject("Geral").getLong("id"));
				questao.setEnunciado(jsonObjectQuestao.getJSONObject("Geral").getString("descricao"));
				questao.setClassificacao("");
				questao.setServico_id(idQuestao);
				
				JSONArray alternativaArray = jsonObjectQuestao.getJSONArray("Alternativa");
				
				List<Alternativa> alternativas = new ArrayList<Alternativa>();
				Alternativa alternativa;
				for (int k=0; k<alternativaArray.length(); k++) {
					alternativa = new Alternativa();
					JSONObject jsonObjectAlternativa = alternativaArray.getJSONObject(k);
					
					alternativa.setDescricao(jsonObjectAlternativa.getString("descricao"));
					alternativa.setCorreta(jsonObjectAlternativa.getInt("correta"));
					
					alternativas.add(alternativa);
					
				}

				questao.setListAlternativa(alternativas);
				this.listQuestoes.add(questao);
	
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


	public List<Servico> getListServicos() {
		return listServicos;
	}
	
 	public String getURL() {
		return URL;
	}

	public void setURL(String uRL) {
		URL = uRL;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	public boolean isReturnServicos() {
		return returnServicos;
	}

	public void setReturnServicos(boolean returnServicos) {
		this.returnServicos = returnServicos;
		if (returnServicos==true){
			String servicos = "http://archimedesonline.com.br/webservice/servicos/"+TOKEN;
			this.setURL(servicos);
		}
		else{
			this.setURL("");
		}
	}
	public void setReturnQuestoes(long id,boolean returnQuestoes ) {
		this.idQuestao = id;
		this.returnQuestoes = returnQuestoes;
		if (returnQuestoes==true){
			String questoes = "http://archimedesonline.com.br/webservice/questoes_teste/"+TOKEN+"/"+String.valueOf(id);
			this.setURL(questoes);
		}
		else{
			this.setURL("");
		}
	}

	public boolean isReturnQuestoes() {
		return returnQuestoes;
	}

	public void setReturnQuestoes(boolean returnQuestoes) {
		this.returnQuestoes = returnQuestoes;
	}
	public List<Questao> getListQuestoes() {
		return listQuestoes;
	}

	public void setListQuestoes(List<Questao> listQuestoes) {
		this.listQuestoes = listQuestoes;
	}
}
